/**
 * 提供一系列的服务
  * ticket:{
   *  time:121331311144,
   *  loginkey:'jiwenfei',
   *  token:'ewsdfdsee12322we//='
 * }
   * 加密方法：
   * 1.把loginkey，time，apppwd（程序间通讯密码）生成一个字符串数组，
   * 2.对这个数组按照字典顺序进行排序
   * 3.按照sh1进行加密
   * 4.进行base64编码。
   * java代码可参考com.xbstar.wei.JFWeiservice的dopost方法
 */

angular.module('starter.services', [])
.factory('StorageService',[function () {
  var gets=function (str_){
    var resstr=localStorage.getItem(str_);
    if(resstr){
      try {
        if(angular.fromJson(resstr)){
        return angular.fromJson(resstr);
      }
      } catch (error) {
        
      }
      try {
         if(JSON.parse(resstr)){
        return JSON.parse(resstr);
      }
      } catch (error) {
        
      }
     
      return resstr;
    }
    else{
      return "";
    }
  };
  var setNameAndHttpDatas=function(name_,httpData_){
    if(name_&&httpData_){
      localStorage.setItem("encodeusername",name_);
      localStorage.setItem("httpData",JSON.stringify(httpData_));
    }else{
      return false;
    }
  }
  var sets=function(key_,obj_){
    
  }
  return{
    
    get:function(key_){
      return gets(key_);
    },
    setNameAndHttpData:function(name_,httpData_){
      setNameAndHttpDatas(name_,httpData_);
    }
  }
}])


/**
 * 一些程序使用工具服务
 */
.factory('APPTools',[function(){
  //md5 加密后的密码
   var MD5 ={
      hexcase : 1,
      b64pad : "",
      chrsz : 8,
      mode : 32,
      md5:function(A){
        return this.hex_md5(A);
      },
      hex_md5:function(A) {
        return this.binl2hex(this.core_md5(this.str2binl(A), A.length * this.chrsz))
      },
      str_md5:function(A) {
        return this.binl2str(this.core_md5(this.str2binl(A), A.length * this.chrsz))
      },
      core_md5:function(K, F) {
        K[F >> 5] |= 128 << ((F) % 32);
        K[(((F + 64) >>> 9) << 4) + 14] = F;
        var J = 1732584193;
        var I = -271733879;
        var H = -1732584194;
        var G = 271733878;
        for (var C = 0; C < K.length; C += 16) {
          var E = J;
          var D = I;
          var B = H;
          var A = G;
          J = this.md5_ff(J, I, H, G, K[C + 0], 7, -680876936);
          G = this.md5_ff(G, J, I, H, K[C + 1], 12, -389564586);
          H = this.md5_ff(H, G, J, I, K[C + 2], 17, 606105819);
          I = this.md5_ff(I, H, G, J, K[C + 3], 22, -1044525330);
          J = this.md5_ff(J, I, H, G, K[C + 4], 7, -176418897);
          G = this.md5_ff(G, J, I, H, K[C + 5], 12, 1200080426);
          H = this.md5_ff(H, G, J, I, K[C + 6], 17, -1473231341);
          I = this.md5_ff(I, H, G, J, K[C + 7], 22, -45705983);
          J = this.md5_ff(J, I, H, G, K[C + 8], 7, 1770035416);
          G = this.md5_ff(G, J, I, H, K[C + 9], 12, -1958414417);
          H = this.md5_ff(H, G, J, I, K[C + 10], 17, -42063);
          I = this.md5_ff(I, H, G, J, K[C + 11], 22, -1990404162);
          J = this.md5_ff(J, I, H, G, K[C + 12], 7, 1804603682);
          G = this.md5_ff(G, J, I, H, K[C + 13], 12, -40341101);
          H = this.md5_ff(H, G, J, I, K[C + 14], 17, -1502002290);
          I = this.md5_ff(I, H, G, J, K[C + 15], 22, 1236535329);
          J = this.md5_gg(J, I, H, G, K[C + 1], 5, -165796510);
          G = this.md5_gg(G, J, I, H, K[C + 6], 9, -1069501632);
          H = this.md5_gg(H, G, J, I, K[C + 11], 14, 643717713);
          I = this.md5_gg(I, H, G, J, K[C + 0], 20, -373897302);
          J = this.md5_gg(J, I, H, G, K[C + 5], 5, -701558691);
          G = this.md5_gg(G, J, I, H, K[C + 10], 9, 38016083);
          H = this.md5_gg(H, G, J, I, K[C + 15], 14, -660478335);
          I = this.md5_gg(I, H, G, J, K[C + 4], 20, -405537848);
          J = this.md5_gg(J, I, H, G, K[C + 9], 5, 568446438);
          G = this.md5_gg(G, J, I, H, K[C + 14], 9, -1019803690);
          H = this.md5_gg(H, G, J, I, K[C + 3], 14, -187363961);
          I = this.md5_gg(I, H, G, J, K[C + 8], 20, 1163531501);
          J = this.md5_gg(J, I, H, G, K[C + 13], 5, -1444681467);
          G = this.md5_gg(G, J, I, H, K[C + 2], 9, -51403784);
          H = this.md5_gg(H, G, J, I, K[C + 7], 14, 1735328473);
          I = this.md5_gg(I, H, G, J, K[C + 12], 20, -1926607734);
          J = this.md5_hh(J, I, H, G, K[C + 5], 4, -378558);
          G = this.md5_hh(G, J, I, H, K[C + 8], 11, -2022574463);
          H = this.md5_hh(H, G, J, I, K[C + 11], 16, 1839030562);
          I = this.md5_hh(I, H, G, J, K[C + 14], 23, -35309556);
          J = this.md5_hh(J, I, H, G, K[C + 1], 4, -1530992060);
          G = this.md5_hh(G, J, I, H, K[C + 4], 11, 1272893353);
          H = this.md5_hh(H, G, J, I, K[C + 7], 16, -155497632);
          I = this.md5_hh(I, H, G, J, K[C + 10], 23, -1094730640);
          J = this.md5_hh(J, I, H, G, K[C + 13], 4, 681279174);
          G = this.md5_hh(G, J, I, H, K[C + 0], 11, -358537222);
          H = this.md5_hh(H, G, J, I, K[C + 3], 16, -722521979);
          I = this.md5_hh(I, H, G, J, K[C + 6], 23, 76029189);
          J = this.md5_hh(J, I, H, G, K[C + 9], 4, -640364487);
          G = this.md5_hh(G, J, I, H, K[C + 12], 11, -421815835);
          H = this.md5_hh(H, G, J, I, K[C + 15], 16, 530742520);
          I = this.md5_hh(I, H, G, J, K[C + 2], 23, -995338651);
          J = this.md5_ii(J, I, H, G, K[C + 0], 6, -198630844);
          G = this.md5_ii(G, J, I, H, K[C + 7], 10, 1126891415);
          H = this.md5_ii(H, G, J, I, K[C + 14], 15, -1416354905);
          I = this.md5_ii(I, H, G, J, K[C + 5], 21, -57434055);
          J = this.md5_ii(J, I, H, G, K[C + 12], 6, 1700485571);
          G = this.md5_ii(G, J, I, H, K[C + 3], 10, -1894986606);
          H = this.md5_ii(H, G, J, I, K[C + 10], 15, -1051523);
          I = this.md5_ii(I, H, G, J, K[C + 1], 21, -2054922799);
          J = this.md5_ii(J, I, H, G, K[C + 8], 6, 1873313359);
          G = this.md5_ii(G, J, I, H, K[C + 15], 10, -30611744);
          H = this.md5_ii(H, G, J, I, K[C + 6], 15, -1560198380);
          I = this.md5_ii(I, H, G, J, K[C + 13], 21, 1309151649);
          J = this.md5_ii(J, I, H, G, K[C + 4], 6, -145523070);
          G = this.md5_ii(G, J, I, H, K[C + 11], 10, -1120210379);
          H = this.md5_ii(H, G, J, I, K[C + 2], 15, 718787259);
          I = this.md5_ii(I, H, G, J, K[C + 9], 21, -343485551);
          J = this.safe_add(J, E);
          I = this.safe_add(I, D);
          H = this.safe_add(H, B);
          G = this.safe_add(G, A)
        }
        if (this.mode == 16) {
          return Array(I, H)
        } else {
          return Array(J, I, H, G)
        }
      },	
      md5_cmn:function(F, C, B, A, E, D) {
        return this.safe_add(this.bit_rol(this.safe_add(this.safe_add(C, F), this.safe_add(A, D)), E), B);
      },
      md5_ff:function md5_ff(C, B, G, F, A, E, D) {
        return this.md5_cmn((B & G) | ((~B) & F), C, B, A, E, D)
      },
      md5_gg:function md5_gg(C, B, G, F, A, E, D) {
        return this.md5_cmn((B & F) | (G & (~F)), C, B, A, E, D)
      },
      md5_hh:function md5_hh(C, B, G, F, A, E, D) {
        return this.md5_cmn(B ^ G ^ F, C, B, A, E, D)
      },
      md5_ii:function md5_ii(C, B, G, F, A, E, D) {
        return this.md5_cmn(G ^ (B | (~F)), C, B, A, E, D)
      },
      bit_rol:function bit_rol(A, B) {
        return (A << B) | (A >>> (32 - B));
      },
      safe_add:function safe_add(A, D) {
        var C = (A & 65535) + (D & 65535);
        var B = (A >> 16) + (D >> 16) + (C >> 16);
        return (B << 16) | (C & 65535)
      },
      
      str2binl:function str2binl(D) {
        var C = Array();
        var A = (1 << this.chrsz) - 1;
        for (var B = 0; B < D.length * this.chrsz; B += this.chrsz) {
          C[B >> 5] |= (D.charCodeAt(B / this.chrsz) & A) << (B % 32)
        }
        return C
      },
      binl2str:function binl2str(C) {
        var D = "";
        var A = (1 << this.chrsz) - 1;
        for (var B = 0; B < C.length * 32; B += this.chrsz) {
          D += String.fromCharCode((C[B >> 5] >>> (B % 32)) & A)
        }
        return D
      },
      binl2hex:function binl2hex(C) {
        var B = this.hexcase ? "0123456789ABCDEF": "0123456789abcdef";
        var D = "";
        for (var A = 0; A < C.length * 4; A++) {
          D += B.charAt((C[A >> 2] >> ((A % 4) * 8 + 4)) & 15) + B.charAt((C[A >> 2] >> ((A % 4) * 8)) & 15)
        }
        return D
      }
    };
/**
 * base64加密
 */
   var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  /**
 * base64解密
 */
    var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                                              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                                              -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
                                              58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0,  1,  2,  3,  4,  5,  6,
                                              7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
                                              25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
                                              37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
                                              -1, -1);
      encodes=function(str){
                var out, i, len;
                var c1, c2, c3;
                len = str.length;
                i = 0;
                out = "";
                while (i < len){
                    c1 = str.charCodeAt(i++) & 0xff;
                    if (i == len){
                        out += base64EncodeChars.charAt(c1 >> 2);
                        out += base64EncodeChars.charAt((c1 & 0x3) << 4);
                        out += "==";
                        break
                     }
                    c2 = str.charCodeAt(i++);
                    if (i == len){
                        out += base64EncodeChars.charAt(c1 >> 2);
                        out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                        out += base64EncodeChars.charAt((c2 & 0xF) << 2);
                        out += "=";
                        break
                     }
                    c3 = str.charCodeAt(i++);
                    out += base64EncodeChars.charAt(c1 >> 2);
                    out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
                    out += base64EncodeChars.charAt(c3 & 0x3F)
          }
          return out
     };
  decodes=function(str){
                var c1, c2, c3, c4;
                var i, len, out;
                len = str.length;
                i = 0;
                out = "";
                while (i < len){
                    do {
                        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
                    } while (i < len && c1 == -1);
                    if (c1 == -1)
                        break;
                    do {
                        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
                     } while (i < len && c2 == -1);
                    if (c2 == -1)
                        break;
                    out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
                    do {
                        c3 = str.charCodeAt(i++) & 0xff;
                        if (c3 == 61)
                            return out;
                        c3 = base64DecodeChars[c3]
                    } while (i < len && c3 == -1);
                    if (c3 == -1)
                        break;
                    out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
                    do {
                        c4 = str.charCodeAt(i++) & 0xff;
                        if (c4 == 61)
                            return out;
                        c4 = base64DecodeChars[c4]
                    } while (i < len && c4 == -1);
                    if (c4 == -1)
                        break;
                    out += String.fromCharCode(((c3 & 0x03) << 6) | c4)
                 }

                return out
        };
  return{
          decode:function(str_){
            return decodes(str_);
          },
          encode:function(str_){
           return  encodes(str_);
          },
          md5:function(den){
            return MD5.md5(den);
          }
          
        }
}])
/**
 * 用于提供用户相关的服务
 * 包含用户认证，用户基本信息。
 */
.factory('UserLoginService', ['$http','$q','StorageService','APPConfig','URLService','APPTools',function($http,$q,StorageService,APPConfig,URLService,AppTools) {
  //登录地址
  //var loginUrl=APPConfig.appLoginUrl+URLService.userLoginUrl.url;
  //登录htpp的方法
  var loginMethod=URLService.userLoginUrl.method;
  //未加密字符串
  var username="";
  var encodeusername="";
  //用户票据
  var ticket={};
  //var serviceUrl="";
  var httpData={};
  var isLogineds=function (name_) {
    username=name_;
    var tmpname=AppTools.encode(name_);
    //1.判断是否在缓存中
    if(name_&&name_!=""&&encodeusername==tmpname){
      return true;
    }else{
      //2.去存储中取
      var tmpname1=StorageService.get("encodeusername");
      if(tmpname1!=null&&tmpname1!=""){
         httpData=StorageService.get("httpData");
         encodeusername=tmpname1;
        return true;
      }else{
        return false;
      }
    }
    
  }
  //var isLogined=false;
  /**
   * 从服务器端获得所需的ticket
   * @param  {string} appname 软件名
   * @param  {String} tmppwd  加密后的密码
   * @param  {String} tmpname 加密后的用户名称 
   */
  var getTicketFromServer=function (appname,tmppwd,tmpname) {
      var deferred = $q.defer(); 
      var loginUrl=APPConfig.appLoginUrl+URLService.userLoginUrl.url;
      $http({
        url:loginUrl,
        method:loginMethod,
        // withCredentials: true,
        headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
        },
        params:{
          appname:appname,
          userpwd:tmppwd,
          loginkey:tmpname
        },
      }).success(function(data,status,config,headers){
        if(data&&data.status&&data.status=="200"){
          httpData=data;
          StorageService.setNameAndHttpData(tmpname,data);
          encodeusername=tmpname;
          deferred.resolve(data.ticket);
        }else{
          if(data&&data.status){
             deferred.reject(data);  
          }else{   
            deferred.reject("400");  
          }
          
        }
      }).error(function(data,status,hedaers,config){
         deferred.reject("500");  
      });
      return deferred.promise;
  };
  
  /**
   * 提供用户登录方法
   * @param  {String} userpwd  未加密用户字符串
   * @param  {String} username  已MD5加密用户密码
   */
  var userLogins=function(userpwd,username){
    var tmpUserName="";
    var tmpPWD="";
    var deferred = $q.defer(); 
    if(username&&userpwd){
      tmpUserName=AppTools.encode(username);
      tmpPWD=AppTools.md5(userpwd);
      tmpPWD=userpwd;
    }else{
      //501:未实现方法，即缺少参数
      deferred.reject("501");
    }
    //1.判断用户是否登录
    if(isLogineds(tmpUserName)){
      deferred.resolve(httpData.ticket);
    }else{
      var appname=APPConfig.appname;
       getTicketFromServer(appname,tmpPWD,tmpUserName)
       .then(function (data) {
          deferred.resolve(data);
       },function (error) {
          deferred.reject(error);
       });
    }
    return deferred.promise; 
  }

  return {
    //用户登录
    //name_
    //pwd_
    userLogin:function(name_,pwd_) {
      return userLogins(pwd_,name_);
    },
    isLogined:function (name_) {
      return isLogineds(name_);
    },
    serviceUrl:function() {
      if(!httpData){
         
         if(isLogineds("")){
           return httpData.serviceurl;
         }
         return "";
      }else{
        return httpData.serviceurl;
       
      }
    },
    ticket:function () {
      return httpData.ticket;
    }
    
  };
}])
.factory('Chats', function() {

  var chats = [{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'You on your way?',
    face: 'img/ben.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Hey, it\'s me',
    face: 'img/max.png'
  }, {
    id: 2,
    name: 'Adam Bradleyson',
    lastText: 'I should buy a boat',
    face: 'img/adam.jpg'
  }, {
    id: 3,
    name: 'Perry Governor',
    lastText: 'Look at my mukluks!',
    face: 'img/perry.png'
  }, {
    id: 4,
    name: 'Mike Harrington',
    lastText: 'This is wicked good ice cream.',
    face: 'img/mike.png'
  }];

  return {
    all: function() {
      return chats;
    },
    remove: function(chat) {
      chats.splice(chats.indexOf(chat), 1);
    },
    get: function(chatId) {
      for (var i = 0; i < chats.length; i++) {
        if (chats[i].id === parseInt(chatId)) {
          return chats[i];
        }
      }
      return null;
    }
  };
})
.factory('localStorageService', [function() {
        return {
            get: function localStorageServiceGet(key, defaultValue) {
                var stored = localStorage.getItem(key);
                try {
                    stored = angular.fromJson(stored);
                } catch (error) {
                    stored = null;
                }
                if (defaultValue && stored === null) {
                    stored = defaultValue;
                }
                return stored;
            },
            update: function localStorageServiceUpdate(key, value) {
                if (value) {
                    localStorage.setItem(key, angular.toJson(value));
                }
            },
            clear: function localStorageServiceClear(key) {
                localStorage.removeItem(key);
            }
        };
    }])
    .factory('dateService', [function() {
        return {
            handleMessageDate: function(messages) {
                var i = 0,
                    length = 0,
                    messageDate = {},
                    nowDate = {},
                    weekArray = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
                    diffWeekValue = 0;
                if (messages) {
                    nowDate = this.getNowDate();
                    length = messages.length;
                    for (i = 0; i < length; i++) {
                        messageDate = this.getMessageDate(messages[i]);
                        if(!messageDate){
                            return null;
                        }
                        if (nowDate.year - messageDate.year > 0) {
                            messages[i].lastMessage.time = messageDate.year + "";
                            continue;
                        }
                        if (nowDate.month - messageDate.month >= 0 ||
                            nowDate.day - messageDate.day > nowDate.week) {
                            messages[i].lastMessage.time = messageDate.month +
                                "月" + messageDate.day + "日";
                            continue;
                        }
                        if (nowDate.day - messageDate.day <= nowDate.week &&
                            nowDate.day - messageDate.day > 1) {
                            diffWeekValue = nowDate.week - (nowDate.day - messageDate.day);
                            messages[i].lastMessage.time = weekArray[diffWeekValue];
                            continue;
                        }
                        if (nowDate.day - messageDate.day === 1) {
                            messages[i].lastMessage.time = "昨天";
                            continue;
                        }
                        if (nowDate.day - messageDate.day === 0) {
                            messages[i].lastMessage.time = messageDate.hour + ":" + messageDate.minute;
                            continue;
                        }
                    }
                    // console.log(messages);
                    // return messages;
                } else {
                    console.log("messages is null");
                    return null;
                }

            },
            getNowDate: function() {
                var nowDate = {};
                var date = new Date();
                nowDate.year = date.getFullYear();
                nowDate.month = date.getMonth();
                nowDate.day = date.getDate();
                nowDate.week = date.getDay();
                nowDate.hour = date.getHours();
                nowDate.minute = date.getMinutes();
                nowDate.second = date.getSeconds();
                return nowDate;
            },
            getMessageDate: function(message) {
                var messageDate = {};
                var messageTime = "";
                //2015-10-12 15:34:55
                var reg = /(^\d{4})-(\d{1,2})-(\d{1,2})\s(\d{1,2}):(\d{1,2}):(\d{1,2})/g;
                var result = new Array();
                if (message) {
                    messageTime = message.lastMessage.originalTime;
                    result = reg.exec(messageTime);
                    if (!result) {
                        console.log("result is null");
                        return null;
                    }
                    messageDate.year = parseInt(result[1]);
                    messageDate.month = parseInt(result[2]);
                    messageDate.day = parseInt(result[3]);
                    messageDate.hour = parseInt(result[4]);
                    messageDate.minute = parseInt(result[5]);
                    messageDate.second = parseInt(result[6]);
                    // console.log(messageDate);
                    return messageDate;
                } else {
                    console.log("message is null");
                    return null;
                }
            }
        };
    }])
    .factory('messageService', ['localStorageService', 'dateService',
        function(localStorageService, dateService) {
            return {
                init: function(messages) {
                    var i = 0;
                    var length = 0;
                    var messageID = new Array();
                    var date = null;
                    var messageDate = null;
                    if (messages) {
                        length = messages.length;
                        for (; i < length; i++) {
                            messageDate = dateService.getMessageDate(messages[i]);
                            if(!messageDate){
                                return null;
                            }
                            date = new Date(messageDate.year, messageDate.month,
                                messageDate.day, messageDate.hour, messageDate.minute,
                                messageDate.second);
                            messages[i].lastMessage.timeFrome1970 = date.getTime();
                            messageID[i] = {
                                id: messages[i].id
                            };

                        }
                        localStorageService.update("messageID", messageID);
                        for (i = 0; i < length; i++) {
                            localStorageService.update("message_" + messages[i].id, messages[i]);
                        }
                    }
                },
                getAllMessages: function() {
                    var messages = new Array();
                    var i = 0;
                    var messageID = localStorageService.get("messageID");
                    var length = 0;
                    var message = null;
                    if (messageID) {
                        length = messageID.length;

                        for (; i < length; i++) {
                            message = localStorageService.get("message_" + messageID[i].id);
                            if(message){
                                messages.push(message);
                            }
                        }
                        dateService.handleMessageDate(messages);
                        return messages;
                    }
                    return null;

                },
                getMessageById: function(id){
                    return localStorageService.get("message_" + id);
                },
                getAmountMessageById: function(num, id){
                    var messages = [];
                    var message = localStorageService.get("message_" + id).message;
                    var length = 0;
                    if(num < 0 || !message) return;
                    length = message.length;
                    if(num < length){
                        messages = message.splice(length - num, length); 
                        return messages;  
                    }else{
                        return message;
                    }
                },
                updateMessage: function(message) {
                    var id = 0;
                    if (message) {
                        id = message.id;
                        localStorageService.update("message_" + id, message);
                    }
                },
                deleteMessageId: function(id){
                    var messageId = localStorageService.get("messageID");
                    var length = 0;
                    var i = 0;
                    if(!messageId){
                        return null;
                    }
                    length = messageId.length;
                    for(; i < length; i++){
                        if(messageId[i].id === id){
                            messageId.splice(i, 1);
                            break;
                        }
                    }
                    localStorageService.update("messageID", messageId);
                },
                clearMessage: function(message) {
                    var id = 0;
                    if (message) {
                        id = message.id;
                        localStorageService.clear("message_" + id);
                    }
                }
            };
        }
    ])


;




/**
 * 登录业务逻辑
 * 1.发送appname，username，userpasswd
 * 2.返回appurl，ticket,
 *  ticket:{
   *  time:121331311144,
   *  loginkey:'jiwenfei',
   *  token:'ewsdfdsee12322we//='
 * }
 * token加密方法：
   * 1.把loginkey，time，apppwd（程序间通讯密码）生成一个字符串数组，
   * 2.对这个数组按照字典顺序进行排序
   * 3.按照sh1进行加密
   * 4.进行base64编码。
   * java代码可参考com.xbstar.wei.JFWeiservice的dopost方法
 * 
 */